package com.global.gbinventory.data.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.global.gbinventory.data.model.Pelanggan;
import com.global.gbinventory.data.tablemodel.PelangganTableModel;
import com.global.gbinventory.data.view.PelangganListFrame;
import com.global.gbinventory.exception.EmptyTableException;
import com.global.gbinventory.exception.NoRowSelectedException;
import com.global.gbinventory.main.controller.MainController;

public class PelangganListController {
	
	private MainController main;
	private PelangganListFrame frame;
	private PelangganTableModel tableModel;
	private PelangganController pelangganController;
	
	private int selectedRow;
	
	public PelangganListController(MainController main){
		this.main = main;
		this.tableModel = new PelangganTableModel(main.getDBMaster());
		selectedRow = -1;
	}
	
	public void start(boolean viewedOnly){
		initialize();
		frame.displayFrame(true, viewedOnly);
		main.addFrameToDesktopPane(frame);
		frame.moveToFront();
	}
	
	private void initialize(){
		this.frame = new PelangganListFrame();
		this.pelangganController = main.getPelangganController();
		addListeners();
		frame.setTableModel(tableModel);
	}
	
	private void addListeners(){
		frame.addBtnOkListener(new BtnOkListener());
		frame.addBtnCancelListener(new BtnCancelListener());
	}
	
	public void setSelectedRow(int selectedRow){
		this.selectedRow = selectedRow;
	}
	
	public int getSelectedRow(){
		return selectedRow;
	}
	
	public void updatePelanggan(String kode, String nama, String alamat, String kota, 
			String kodepos,String provinsi, String telepon, String hp, String email, String keterangan){
		
		tableModel.updatePelanggan(selectedRow, kode, nama, alamat, kota, kodepos, provinsi, 
				telepon, hp, email, keterangan);
	
	}
	
	public void removePelanggan(){
		tableModel.removePelanggan(selectedRow);
	}
	
	public String isExistedKode(String kode){
		return tableModel.isExistedKode(kode);
	}

	class BtnOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try{
				selectedRow = frame.getTableSelectedRow();
				if(tableModel.isEmptyTable()){
					throw new EmptyTableException();
				}
				
				if(selectedRow < 0){
					throw new NoRowSelectedException();
				}
				
				Pelanggan pelanggan = tableModel.getPelanggan(selectedRow);
				String kode = pelanggan.getKode();
				String nama = pelanggan.getNama();
				String alamat = pelanggan.getAlamat();
				String kota = pelanggan.getKota();
				String kodepos = pelanggan.getKodepos();
				String provinsi = pelanggan.getProvinsi();
				String telepon = pelanggan.getTelepon();
				String hp = pelanggan.getHp();
				String email = pelanggan.getEmail();
				String keterangan = pelanggan.getKeterangan();
				
				pelangganController.fillData(kode, nama, alamat, kota, kodepos, provinsi, 
						telepon, hp, email, keterangan);
				frame.disposeFrame();
				main.removeFrameFromDesktopPane(frame);
				
				pelangganController.activateEditDelete();
				
			} catch(NoRowSelectedException e1){
				e1.printError(frame);
				
			} catch(EmptyTableException e2){
				e2.printError(frame);
			}
			
		}
		
	}
	
	class BtnCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			frame.disposeFrame();
			main.removeFrameFromDesktopPane(frame);
		}
		
	}
	
}
